先把需要的檔案叫出來(etf4_csv、etf4.c、etf4.reorder、etf4.xts、etf4_monthly、etf4_returns_xts)。

01:最後會顯示出折線圖。

橫軸為日期。

library(readr)
etf4_csv<-read_csv("ETF4_2000_2018_d.csv", locale = locale(encoding='big5'))
## Parsed with column specification:
## cols(
##   證券代碼 = col_character(),
##   簡稱 = col_character(),
##   日期 = col_double(),
##   `未調整收盤價(元)` = col_double(),
##   `當日均價(元)` = col_double()
## )
etf4.c<-etf4_csv[, c(-2, -4)]
etf4.c<-etf4.c[-1,]
colnames(etf4.c)<-c("id", "date", "price")
library(reshape2)
etf4.reorder = dcast(etf4.c, date~id)
## Using price as value column: use value.var to override.
etf4.reorder$date<-as.Date(as.character(etf4.reorder$date), "%Y%m%d") 
dim(etf4.reorder)
## [1] 2474    5
head(etf4.reorder)
##         date  0050  0056 006205 00646
## 1 2009-01-05    NA 14.00     NA    NA
## 2 2009-01-06 34.21 14.02     NA    NA
## 3 2009-01-07 34.59 14.28     NA    NA
## 4 2009-01-08 33.21 13.86     NA    NA
## 5 2009-01-09 32.32 13.61     NA    NA
## 6 2009-01-10 31.91 13.55     NA    NA
str(etf4.reorder)
## 'data.frame':    2474 obs. of  5 variables:
##  $ date  : Date, format: "2009-01-05" "2009-01-06" ...
##  $ 0050  : num  NA 34.2 34.6 33.2 32.3 ...
##  $ 0056  : num  14 14 14.3 13.9 13.6 ...
##  $ 006205: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ 00646 : num  NA NA NA NA NA NA NA NA NA NA ...
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
etf4.xts<-xts(etf4.reorder[,-1], order.by= etf4.reorder$date)
head(etf4.xts)
##             0050  0056 006205 00646
## 2009-01-05    NA 14.00     NA    NA
## 2009-01-06 34.21 14.02     NA    NA
## 2009-01-07 34.59 14.28     NA    NA
## 2009-01-08 33.21 13.86     NA    NA
## 2009-01-09 32.32 13.61     NA    NA
## 2009-01-10 31.91 13.55     NA    NA
tail(etf4.xts)
##             0050  0056 006205 00646
## 2018-12-22 74.75 24.15  25.08 22.93
## 2018-12-24 74.67 24.16  25.25 22.72
## 2018-12-25 73.57 23.90  24.90 22.51
## 2018-12-26 73.87 23.83  25.16 22.13
## 2018-12-27 74.81 23.96  25.30 22.95
## 2018-12-28 75.21 23.92  25.24 23.16
str(etf4.xts)
## An 'xts' object on 2009-01-05/2018-12-28 containing:
##   Data: num [1:2474, 1:4] NA 34.2 34.6 33.2 32.3 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:4] "0050" "0056" "006205" "00646"
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
library(quantmod)
## Warning: package 'quantmod' was built under R version 3.5.3
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.5.3
## Version 0.4-0 included new data defaults. See ?getSymbols.
etf4_monthly <- to.monthly(etf4.xts, indexAt = "lastof", OHLC=FALSE)
## Warning in to.period(x, "months", indexAt = indexAt, name = name, ...):
## missing values removed from data
head(etf4_monthly)
##             0050  0056 006205 00646
## 2015-12-31 60.75 21.81  31.97 20.07
## 2016-01-31 59.51 21.48  26.21 19.21
## 2016-02-29 61.25 22.39  25.90 19.22
## 2016-03-31 64.82 22.44  27.83 19.73
## 2016-04-30 61.63 21.58  27.17 19.82
## 2016-05-31 63.10 21.86  27.10 20.31
library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 3.5.3
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(magrittr)
etf4_returns_xts <-Return.calculate(etf4_monthly, method = "log") %>%
  na.omit()
head(etf4_returns_xts)
##                   0050        0056       006205         00646
## 2016-01-31 -0.02062272 -0.01524631 -0.198656946 -0.0437951860
## 2016-02-29  0.02881948  0.04149216 -0.011898049  0.0005204268
## 2016-03-31  0.05665035  0.00223065  0.071871607  0.0261889165
## 2016-04-30 -0.05046543 -0.03907812 -0.024001152  0.0045512089
## 2016-05-31  0.02357200  0.01289152 -0.002579696  0.0244218467
## 2016-06-30  0.03335201  0.03196311 -0.017119882 -0.0249265149
dim(etf4_returns_xts)
## [1] 36  4
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
plot(etf4_returns_xts, xaxt='n')
axis(1, index(etf4_returns_xts), format(index(etf4_returns_xts), "%Y/%m"))

02:利用ggplot2,fortify使物件轉為data.frame。

橫軸為0050,縱軸為00646S&P500。

library(ggplot2)
etf4_ret.df1<-fortify(etf4_returns_xts)
head(etf4_ret.df1)
##        Index        0050        0056       006205         00646
## 1 2016-01-31 -0.02062272 -0.01524631 -0.198656946 -0.0437951860
## 2 2016-02-29  0.02881948  0.04149216 -0.011898049  0.0005204268
## 3 2016-03-31  0.05665035  0.00223065  0.071871607  0.0261889165
## 4 2016-04-30 -0.05046543 -0.03907812 -0.024001152  0.0045512089
## 5 2016-05-31  0.02357200  0.01289152 -0.002579696  0.0244218467
## 6 2016-06-30  0.03335201  0.03196311 -0.017119882 -0.0249265149
plot(etf4_ret.df1$`0050`, etf4_ret.df1$`00646`, pch=20,
     col = 'darkred', main = '0050 vs. 00646 monthly returns',
     xlab = '0050', ylab = '00646 S&P500')

03:利用lubridate。

橫軸為etf4_ret.df2$0050,縱軸為etf4_ret.df2$0056。

點的顏色由深藍色到淺藍色為3月、6月、9月、12月。

library(lubridate)
## Warning: package 'lubridate' was built under R version 3.5.3
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
etf4_ret.df2 <- cbind(etf4_ret.df1, month=month(index(etf4_returns_xts)), 
                year=year(index(etf4_returns_xts)))
ggplot(data = etf4_ret.df2) +
  geom_point(mapping = aes(x = etf4_ret.df2$`0050`, y = etf4_ret.df2$`0056`, color = month))

04:

橫軸為etf4_ret.df2$0050,縱軸為etf4_ret.df2$0056。

點的大小由小到大為3月、6月、9月、12月。

ggplot(data = etf4_ret.df2) +
  geom_point(mapping = aes(x = etf4_ret.df2$`0050`, y = etf4_ret.df2$`0056`, size = month))

05:

橫軸為etf4_ret.df2$0050,縱軸為etf4_ret.df2$0056。

點的alpha粒子顏色由淺到深為3月、6月、9月、12月。

ggplot(data = etf4_ret.df2) +
  geom_point(mapping = aes(x = etf4_ret.df2$`0050`, y = etf4_ret.df2$`0056`, alpha = month))

06:

圖為折線圖,橫軸為Index,縱軸為Value。

粉紅色為0050,綠色為0056,藍色為006205,紫色為00646。

etf4_ret.df<-fortify(etf4_returns_xts, melt=TRUE)
head(etf4_ret.df)
##        Index Series       Value
## 1 2016-01-31   0050 -0.02062272
## 2 2016-02-29   0050  0.02881948
## 3 2016-03-31   0050  0.05665035
## 4 2016-04-30   0050 -0.05046543
## 5 2016-05-31   0050  0.02357200
## 6 2016-06-30   0050  0.03335201
p<-ggplot(etf4_ret.df, aes(x = Index, y = Value))+
  geom_line(aes(color = Series), size = 1)

p + scale_x_date(date_labels = "%Y/%m")

07:標題為Monthly Returns。

圖為直條圖。

粉紅色為0050,綠色為0056,藍色為006205,紫色為00646。

q<-etf4_ret.df %>%
  ggplot(aes(x =Value, fill = Series)) +
  geom_histogram(alpha = 0.45, binwidth = .005) +
  ggtitle("Monthly Returns")
q + facet_wrap(~Series)+ theme_update(plot.title = element_text(hjust = 0.5))

08:標題為Monthly Returns Density Since 2016。

橫軸為monthly returns,縱軸為distribution。

粉紅色為0050,綠色為0056,藍色為006205,紫色為00646。

etf4_ret.df %>%
  ggplot(aes(x = Value, colour = Series)) +
  geom_density(alpha = 1) +
  ggtitle("Monthly Returns Density Since 2016") +
  xlab("monthly returns") +
  ylab("distribution") +
  theme_update(plot.title = element_text(hjust = 0.5))

09:將07、08的圖合併起來。

圖的標題為Monthly Returns Since 2016。

橫軸為monthly returns,縱軸為distribution。

粉紅色為0050,綠色為0056,藍色為006205,紫色為00646。

etf4_ret.df %>%
  ggplot(aes(x = Value)) +
  geom_density(aes(color = Series), alpha = 1) +
  geom_histogram(aes(fill = Series), alpha = 0.45, binwidth = .01) +
  guides(fill = FALSE) +
  facet_wrap(~Series) +
  ggtitle("Monthly Returns Since 2016") +
  xlab("monthly returns") +
  ylab("distribution") +
  theme_update(plot.title = element_text(hjust = 0.5))

10:

橫軸為年,縱軸為monthly returns。

藍色為0050,橘色為0056。

library(PerformanceAnalytics)
library(magrittr)
etf4_ret<-coredata(etf4_monthly[-1,])/coredata(etf4_monthly[-dim(etf4_monthly)[1],])-1
head(etf4_ret)
##             0050        0056       006205         00646
## [1,] -0.02041152 -0.01513067 -0.180168908 -0.0428500249
## [2,]  0.02923878  0.04236499 -0.011827547  0.0005205622
## [3,]  0.05828571  0.00223314  0.074517375  0.0265348595
## [4,] -0.04921321 -0.03832442 -0.023715415  0.0045615813
## [5,]  0.02385202  0.01297498 -0.002576371  0.0247225025
## [6,]  0.03391442  0.03247941 -0.016974170 -0.0246184146
class(etf4_ret)
## [1] "matrix"
etf4_ret
##                0050         0056       006205         00646
##  [1,] -0.0204115226 -0.015130674 -0.180168908 -0.0428500249
##  [2,]  0.0292387834  0.042364991 -0.011827547  0.0005205622
##  [3,]  0.0582857143  0.002233140  0.074517375  0.0265348595
##  [4,] -0.0492132058 -0.038324421 -0.023715415  0.0045615813
##  [5,]  0.0238520201  0.012974977 -0.002576371  0.0247225025
##  [6,]  0.0339144216  0.032479414 -0.016974170 -0.0246184146
##  [7,]  0.0554874310  0.080194949  0.012012012  0.0312973246
##  [8,]  0.0124891083  0.013125513  0.024480712  0.0024473813
##  [9,]  0.0192197361  0.013765182 -0.038015930 -0.0219726562
## [10,]  0.0153391500 -0.053913738  0.020700038 -0.0054917624
## [11,] -0.0059598060 -0.018151119  0.057522124  0.0411646586
## [12,]  0.0006971556 -0.008598452 -0.058577406  0.0385728062
## [13,]  0.0197854257  0.019947962  0.018148148 -0.0129990715
## [14,] -0.0012296762  0.039115646  0.003273918  0.0145813735
## [15,]  0.0103967168  0.013093290 -0.031182016  0.0000000000
## [16,]  0.0083942594 -0.012116317 -0.015344311 -0.0023180343
## [17,]  0.0296723953  0.015944399  0.035347777  0.0041821561
## [18,]  0.0445951232  0.027766600  0.060939794  0.0143452106
## [19,]  0.0154787168  0.021534847  0.043944637  0.0159671533
## [20,]  0.0195451752  0.000000000  0.038448790 -0.0094297261
## [21,] -0.0227875573 -0.006132618 -0.006383658  0.0385312783
## [22,]  0.0502159161 -0.015426147  0.024734982  0.0139676997
## [23,] -0.0300751880 -0.020368194  0.010344828  0.0111924236
## [24,] -0.0064195736  0.000000000 -0.004964319  0.0166028097
## [25,]  0.0295014019  0.053178729  0.082008107  0.0335008375
## [26,] -0.0197750148 -0.012528474 -0.036599424 -0.0113452188
## [27,]  0.0013288234 -0.001922338 -0.056536045 -0.0385245902
## [28,] -0.0384847388 -0.030046225 -0.016487001  0.0166240409
## [29,]  0.0127979925  0.040508340 -0.001611863  0.0310272537
## [30,]  0.0012388503 -0.023664122 -0.083629319  0.0166734445
## [31,]  0.0570403366  0.057857701  0.009866103  0.0340000000
## [32,]  0.0155683015  0.022172949 -0.038381019  0.0398452611
## [33,]  0.0051867220 -0.013738250  0.030478955  0.0000000000
## [34,] -0.1145510836 -0.130865103 -0.076408451 -0.0662202381
## [35,] -0.0012950013  0.032053986  0.001143729  0.0139442231
## [36,] -0.0247665975 -0.022476502 -0.038842346 -0.0899803536
etf4_ret.tmp<-data.frame(date = index(etf4_returns_xts), etf4_ret)
head(etf4_ret.tmp)
##         date       X0050       X0056      X006205        X00646
## 1 2016-01-31 -0.02041152 -0.01513067 -0.180168908 -0.0428500249
## 2 2016-02-29  0.02923878  0.04236499 -0.011827547  0.0005205622
## 3 2016-03-31  0.05828571  0.00223314  0.074517375  0.0265348595
## 4 2016-04-30 -0.04921321 -0.03832442 -0.023715415  0.0045615813
## 5 2016-05-31  0.02385202  0.01297498 -0.002576371  0.0247225025
## 6 2016-06-30  0.03391442  0.03247941 -0.016974170 -0.0246184146
library(plotly)
## Warning: package 'plotly' was built under R version 3.5.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p1 = plot_ly(etf4_ret.tmp, x= ~date, y= ~X0050, name = "0050", type = "scatter", mode = "lines") %>%
  add_trace(y=~X0056, name = '0056', mode = 'lines+markers') %>%
  layout(xaxis = list(title = 'year'), yaxis = list(title = 'monthly returns'))
p1